package com.vincent.common.utils;

import org.springframework.util.Assert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Vincent
 * @description 扩展的断言
 * @version 1.0
 */
public class AssertUtil extends Assert {
    /**
     * 判断是否满足正则表达式
     * @param obj
     * @param reg
     */
    public static void matchReg(String obj, String reg){
        Pattern pattern=Pattern.compile(reg);
        Matcher matcher = pattern.matcher(obj);
        if(!matcher.find()){
            throw new IllegalArgumentException(obj+" don not match the reg:"+reg);
        }
    }
    /**
     * 判断是否满足正则表达式
     * @param obj
     * @param reg
     */
    public static void matchReg(String obj,String reg,String msg){
        Pattern pattern=Pattern.compile(reg);
        Matcher matcher = pattern.matcher(obj);
        if(!matcher.find()){
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * 判断数组长度是否大于0
     * @param array
     */
    public static void hasArrayLength(Object []array){
        if(array.length<=0){
            throw new IllegalArgumentException("the length of array is 0");
        }
    }

    /**
     * 判断数组长度是否大于0 自定义错误信息
     * @param array
     * @param msg
     */
    public static void hasArrayLength(Object []array,String msg){
        if(array.length<=0){
            throw new IllegalArgumentException(msg);
        }
    }
}
